<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 */

namespace App\Http\Controllers\Drivers\V2\Order;

use App\Http\Controllers\Controller;
use App\Services\DriversOrderService;
use App\Services\DriversPostage;
use App\Services\UsersService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class OrderController extends Controller
{
    /**
     * 订单列表
     */
    public function lists(Request $request, UsersService $usersService, DriversOrderService $service)
    {
        $page = $request->post('page', $this->page);
        $size = $request->post('size', $this->size);
        $driver_id = $usersService::driver_id(Auth::user()); //获取司机ID
        $info = $service->drivers_order_list($driver_id, $page, $size);
        return $this->sendJson($info);
    }

    /**
     * 订单详情
     */
    public function order_details(Request $request, DriversOrderService $service)
    {
        $order_id = (int)$request->post('order_id'); //订单ID
        if (empty($order_id)) return_bobc('订单ID不能为空');
        return $this->sendJson($service->drivers_order_details($order_id));
    }

    /**
     * 取消订单
     */
    public function cancellation_of_order(Request $request, DriversOrderService $service)
    {
        $order_id = (int)$request->post('order_id');
        if (empty($order_id)) return_bobc('订单ID必传');
        $res = $service->cancellation_of_order($order_id);
        if ($res <> 1) return_bobc('取消失败');
        return $this->sendJson('取消成功');
    }

    /**
     * 订单详情
     */
    public function order_details_show(Request $request, DriversOrderService $service)
    {
        $order_id = (int)$request->post('order_id');
        return $this->sendJson($service->order_details_show($order_id,Auth::user()));
    }

    /**
     * 司机发起收款补充费用信息
     * 过路费
     * 停车费
     */
    public function InitiateCollection(Request $request, DriversOrderService $service)
    {
        $order_id = (int)$request->post('order_id'); //订单ID
        $road_toll = $request->post('road_toll') ?: 0; //过路费
        $parking = $request->post('parking') ?: 0;  //停车费
        if (empty($order_id)) return_bobc('订单ID必须');
        $infos = $service->ArriveAtTheBoardingPlacePy($order_id, $road_toll, $parking);
        return $this->sendJson($infos);
    }

    /**
     * @param Request $request
     * @param DriversOrderService $service
     * 选单
     */
    public function driverMenu(Request $request, DriversOrderService $service, UsersService $usersService)
    {
        $lat = (string)$request->post('lat');
        $lng = (string)$request->post('lng');
        $car_id = (int)$request->post('car_id');
        $type = (string)$request->post('type', '0'); // 0 网约车  1 代驾
        if (empty($lat)) return_bobc('司机经度必传');
        if (empty($lng)) return_bobc('司机纬度必传');
        $list = $service->driverMenu(Auth::user(), $lng, $lat, $car_id, $type);
        return $this->sendJson($list);
    }

    /**
     * 司机所得费用计算
     */
    public function DriverOrderSettlement(Request $request, DriversPostage $postage)
    {
        $order_id = (int)$request->post('order_id'); //订单ID
        if (empty($order_id)) return $this->sendError('订单ID必须');
        return $this->sendJson($postage->DriverOrderSettlement($order_id));
    }

    /**
     * 司机确认账单
     */
    public function DriverConfirmsBill(Request $request, DriversOrderService $service)
    {
        $order_id = (int)$request->post('order_id'); //订单ID
        if (empty($order_id)) return_bobc('订单ID必传');
        $type = $service->order_type($order_id);
        if ($type == '0') {
            return $this->sendJson($service->DriverConfirmsBill($order_id));
        }
        return $this->sendJson($service->drivingDriverConfirmsBill($order_id));
    }
}